package com.qpf.mycms.component.auth.configuration;

import com.qpf.mycms.api.PasswordServiceApi;
import com.qpf.mycms.component.auth.service.SysUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authorization.AuthorizationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;

import javax.annotation.Resource;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
    @Resource
    private PasswordServiceApi passwordServiceApi;
    @Resource
    private AuthenticationManager authenticationManagerBean;
    @Resource
    private UserDetailsService sysUserDetailsService;

    /**
     * 密码模式需要auth2授权服务器去校验账号和密码
     * 账号和密码在整合security放在security中了
     * 所以需要整合security的授权管理器authenticationManager
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        //使用密码模式需要配置
        endpoints.authenticationManager(authenticationManagerBean)
                // 指定token存储方式
                .tokenStore(tokenStore())
                // refresh_token是否重复使用
                .reuseRefreshTokens(false)
                .userDetailsService(sysUserDetailsService)
                // 仅允许POST请求
                .allowedTokenEndpointRequestMethods(HttpMethod.POST)
                ;
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        // 校验token需要带入clentId和clientSecret
        security.checkTokenAccess("isAuthenticated()");
    }
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("client")
                .secret(passwordServiceApi.encode("secret"))
                .accessTokenValiditySeconds(3600)
                .refreshTokenValiditySeconds(3600*24*7)
                .authorizedGrantTypes("authorization_code", "password", "refresh_token", "client_credentials")
                .scopes("all")
                .autoApprove(true)
                .redirectUris("https://www.baidu.com");
    }

    @Bean
    public TokenStore tokenStore() {
        return new InMemoryTokenStore();
    }
}
